﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace AppTouchScreen.Views
{
    /// <summary>
    /// Provide animation for Button.Visibility
    /// </summary>
    public class EditButtonStoryboard
    {
        public static Storyboard GetChangeStoryboard(Button obj) { return (Storyboard)obj.GetValue(ChangeStoryboardProperty); }

        public static void SetChangeStoryboard(Button obj, Storyboard value) { obj.SetValue(ChangeStoryboardProperty, value); }

        public static readonly DependencyProperty ChangeStoryboardProperty =
            DependencyProperty.RegisterAttached("ChangeStoryboard", typeof(Storyboard), typeof(EditButtonStoryboard),
            new PropertyMetadata(null, ChangeStoryboardChanged));

        private static void ChangeStoryboardChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var prop = DependencyPropertyDescriptor.FromProperty(Button.VisibilityProperty, typeof(Button));
            if (prop == null) return;
            if (e.NewValue != null)
                prop.AddValueChanged(sender, btn_SourceChanged);
            else
                prop.RemoveValueChanged(sender, btn_SourceChanged);
        }

        private static void btn_SourceChanged(object sender, EventArgs e)
        {
            var btn = (Button)sender;
            Storyboard sb = GetChangeStoryboard(btn);
            if (sb != null) // на всякий случай проверим
                sb.Begin(btn); // анимация всегда будет назначаться непосредственно image
        }

    }
}
